package com.forlove.app.qselect.answer.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.answer.service.ILimitAnswerService;
import com.forlove.app.qselect.answer.service.impl.LimitAnswerServiceImpl;
import com.forlove.app.qselect.answer.view.LimitAnswerView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2018/1/5.
 */

public class LimitAnswerPresenter extends IBasePresenter<ILimitAnswerService> {
    private ILimitAnswerService limitAnswerService;
    private LimitAnswerView limitAnswerView;

    public LimitAnswerPresenter(LimitAnswerView limitAnswerView) {

        this.limitAnswerView = limitAnswerView;
        if (limitAnswerService == null) {
            limitAnswerService = new LimitAnswerServiceImpl();
        }
    }


    public void loadResetQuestionData(boolean diamondUnlock) {
        Map param = new HashMap();
        if (diamondUnlock) {
            param.put("useDiamond", 1);
        }
        limitAnswerService.loadData(ServerManager.ACTION_RESET_QUESTION, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "重置答题:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                AnswerEntity answerEntity = ParseHelper.getModel(result, "data", AnswerEntity.class);
                limitAnswerView.showResetAnswerView(answerEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (limitAnswerView != null) {
                        limitAnswerView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (limitAnswerView != null) {
                        limitAnswerView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
